package com.innoking.docsys.workflow.controller;

import com.innoking.docsys.utils.JsonToOtherUtil;
import com.innoking.docsys.utils.OtherPojo;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.workflow.module.WorkflowDeploymentVO;
import com.innoking.docsys.workflow.service.WorkflowDeployService;
import org.activiti.engine.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 描述:
 *
 * @author zyp
 * @description 工作流程的部署与定义控制器
 * @create 2019-09-06 14:03
 */
@RestController
@RequestMapping("/workflowDeploy")
public class WorkflowDeployController {

    Logger logger= LoggerFactory.getLogger(WorkflowDeployController.class);

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    private WorkflowDeployService workflowDeployService;

    /**
     * @author:zyp
     * @description 获取部署信息
     * @date: 2019/9/9 11:03
     * @return
     */
    @PostMapping("/getActDeploy")
    @ResponseBody
    public ResultObjVO getActDeploy(@RequestBody String data) {
        ResultObjVO result = workflowDeployService.getActDeploy(data);
        return result;
    }

    /**
     * @author:zyp
     * @description 读取资源，通过部署ID
     * @date: 2019/2/26 9:43
     * @param deploymentId 流程定义
     * @param resourceType        资源类型(xml|image)
     * @return
     */
    @PostMapping(value = "/loadByDeployment")
    public void loadByDeployment(@RequestBody String data,HttpServletResponse response) throws Exception {
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new WorkflowDeploymentVO() );
        WorkflowDeploymentVO workflowDeploymentVO = (WorkflowDeploymentVO)ob.getObj();
        workflowDeployService.loadByDeployment(workflowDeploymentVO.getDeploymentId(),workflowDeploymentVO.getResourceType(),response);
    }

    /**
     * @author:zyp
     * @description 删除部署的流程，级联删除流程实例
     * @date: 2019/9/9 11:40
     * @return
     */
    @PostMapping(value = "/deleteDeploy")
    @ResponseBody
    public ResultObjVO deleteDeploy(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new WorkflowDeploymentVO() );
        WorkflowDeploymentVO workflowDeploymentVO = (WorkflowDeploymentVO)ob.getObj();
        ResultObjVO resultObjVO = workflowDeployService.deleteDeploy(workflowDeploymentVO.getDeploymentIds());
        return resultObjVO;
    }

    /**
     * @author:zyp
     * @description 根据部署id转换为Model
     * @date: 2019/9/9 11:47
     * @return
     */
    @PostMapping(value = "/convertToModel")
    @ResponseBody
    public ResultObjVO convertToModel(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new WorkflowDeploymentVO() );
        WorkflowDeploymentVO workflowDeploymentVO = (WorkflowDeploymentVO)ob.getObj();
        ResultObjVO resultObjVO = workflowDeployService.convertToModel(workflowDeploymentVO.getDeploymentIds());
        return resultObjVO;
    }

    /**
     * @author:zyp
     * @description 文件导入流程模型 同时部署 及定义
     * @date: 2019/9/9 11:57
     * @return
     */
    @PostMapping(value = "/deployByFile")
    @ResponseBody
    public ResultObjVO deployByFile(@RequestParam(value = "file", required = false) MultipartFile file,String defineCode) {
        ResultObjVO resultObjVO = workflowDeployService.deployByFile(file,defineCode);
        return resultObjVO;
    }

    /**
     * @author:zyp
     * @description 挂起、激活流程
     * @date: 2019/9/9 16:10
     * @return
     */
    @PostMapping(value = "/updateDeployState")
    @ResponseBody
    public Object updateState(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new WorkflowDeploymentVO() );
        WorkflowDeploymentVO workflowDeploymentVO = (WorkflowDeploymentVO)ob.getObj();
        ResultObjVO resultObjVO = workflowDeployService.updateState(workflowDeploymentVO.getState(),workflowDeploymentVO.getDeploymentId());
        Map<String,String> map = new HashMap<String,String>();
        return  resultObjVO;
    }

}